package com.wb.code09集合.练习;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 黄衡熙
 * @version 1.0
 */
// 提示用户输入5名学生的学号和成绩,创建Student对象(属性:no、score),然后放到ArrayList集合中

public class Test02_ {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        for(int i=1;i<=5;i++){
            System.out.print("请输入第"+i+"个学生的学号:");
            String no = sc.next();
            System.out.print("请输入学生的成绩:");
            double score = sc.nextDouble();
            list.add(new Student(no, score));
        }
        System.out.println(list);
        // 1.遍历集合,删除学号为2的学生
        for(int i= list.size()-1;i>=0;i--){
            if("2".equals(list.get(i).getNo())){
                list.remove(i);
                System.out.println("已删除学号为2的学生");
            }
        }
        System.out.println(list);
        // 2.不遍历集合,删除学号为3的学生
        Student target3 = new Student("3", 0);
        boolean stu = list.remove(target3);
        if(stu){
            System.out.println("已删除学号为3的学生");
        }else{
            System.out.println("未找到学号为3的学生");
        }
        System.out.println("删除学号3后: " + list);
      // 3.不遍历集合,判断集合中是否包含学号为5的学生
        Student target5 = new Student("5", 0);
        boolean contains = list.contains(target5);
        System.out.println("集合中" + (contains ? "包含" : "不包含") + "学号为5的学生");
        System.out.println(list);
    }
}
class Student{
    private String no;
    private double score;
    public Student(String no, double score) {
        this.no = no;
        this.score = score;
    }
    public String toString() {
        return "{no='" + no + "', score=" + score + '}';
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Student student = (Student) obj;
        return no != null && no.equals(student.no);
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}